const db=wx.cloud.database().collection('cartList')

Page({
  data:{
cart:[],
allChecked:false,
    totalPrice: 0,
    totalNum: 0,
    address:{}
  },
  onShow:function() {
    let {totalNum,totalPrice} = this.data
    totalNum=0,
    totalPrice=0
    this.setData({
      totalNum,totalPrice
    })
     this.getCartList()
  },
  // 获取数据库中的购物车商品
 async  getCartList() {
           let  that=this
           let openid=await wx.getStorage({
             key: 'openid',
           })
           openid=openid.data
          //  console.log(openid);
           let {cart}= this.data
           db.where({
             _openid:openid
           })
           .get()
           .then(res=>{
             console.log(res);
             cart = res.data
            //  console.log(this.data.cart);
             console.log(cart);
             this.setData({
               cart
             })
           })
   },
  //  复选框
  handleChenckedChange(e) {
      const goods_id = e.currentTarget.dataset.id
      let {cart} = this.data
      let index = cart.findIndex(v=>v.goods_id===goods_id)
      // 复选框取反
      cart[index].checked = !cart[index].checked
      // console.log(cart[index]);
     if( cart.every(v=>v.checked) ) {
       this.data.allChecked=true
     } else {
       this.data.allChecked=false
     }
    //  console.log(this.data.allChecked);
    this.setData({
      allChecked:this.data.allChecked
    })
    this.setCart(cart)
  },
  // 全选框
  handleAllCheckedChange(e) {
    let that=this
    this.data.allChecked=!this.data.allChecked
    // console.log(this.data.allChecked);
    let cart=this.data.cart
    cart.forEach(v=>{
      v.checked = that.data.allChecked
      console.log(v.checked);
    })
    this.setData({
      cart
    })
    this.setCart(cart)
  },
  // 减少按钮
  handleGoodsDelete(e) {
    const goods_id = e.currentTarget.dataset.id
      let {cart} = this.data
      let index = cart.findIndex(v=>v.goods_id===goods_id)
    //  console.log(cart[index]);
     if(cart[index].num===1) {
      wx.showModal({
                title: '提示',
                content: '您是否要删除该商品？',
                success: (res) => {
                  if (res.confirm) {
                    cart.splice(index,1)
                    db.where({
                      goods_id:goods_id
                    }).remove()
                  this.setData({cart})
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
     } else {
            cart[index].num--
            db.where({
              goods_id:goods_id
            }).update({
              data:{
                num:cart[index].num
              }
            })
            this.setData({cart})
        }
        this.setCart(cart)
  },
  // 增加功能
  handleGoodsAdd(e) {
    const goods_id = e.currentTarget.dataset.id
    const {cart} =this.data
    let index = cart.findIndex(v=>v.goods_id===goods_id)
    cart[index].num++
    db.where({
      goods_id:goods_id
    }).update({
      data:{
        num:cart[index].num
      }
    })
    this.setData({cart})
    this.setCart(cart)
  },
    // 设置购物车状态 重新计算底部数据
  setCart(cart) {
      let allChecked = true
      let totalPrice = 0
      let totalNum = 0
      cart.forEach(v => {
        if (v.checked) {
          totalPrice += v.goods_price * v.num
          totalNum += v.num
        } else {
          allChecked = cart.every(v => v.checked)
        }
      })
      allChecked = cart.length ? allChecked : false
      this.setData({
        cart,totalPrice,totalNum, allChecked
      })
  },

 // 获取地址功能
  handleChooseAddress() {
   wx.chooseAddress({
      success: (result) => {
        //  console.log(result);
        const address = result
        // console.log(address);
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
        wx.setStorageSync('address', address)
      }
    })
  },
  // 跳转到结算界面
  handlePay() {
    let {totalNum} =this.data
    if(totalNum===0) {
      wx.showToast({
        title: '您还没有选择商品',
        icon:'none'
      })
    } else {
      wx.navigateTo({
        url: '/pages/pay/pay?cart='+JSON.stringify(this.data.cart),
      })
    }
  }
})
